<?php
    class Ruangan_con extends CI_Controller{
        function __construct() {
            parent::__construct();
        }
        function index(){
            $this->tampil_ruangan();
        }
        function tampil_ruangan(){
            $query=$this->db->get('daftar_ruangan');
            if($query->num_rows()>0){
                $this->load->library('table');
                $this->table->set_template(array('table_open' => '<table border="1" cellpadding="4" cellspacing="3">'));
                $this->table->set_heading('NO', 'NAMA RUANGAN', 'ID RUANGAN','KAPASISTAS','AKSI');
                $i=1;
                foreach ($query->result() as $entry) {
                    $this->table->add_row(
                            $i, $entry->NAMA_RUANGAN, $entry->ID_RUANGAN, $entry->KAPASITAS, anchor(base_url() . 'admin/ruangan_con/edit_ruangan/' . $entry->ID_RUANGAN, 'Edit') .
                            ' | ' .
                            anchor(base_url() . 'admin/ruangan_con/hapus_ruangan/' . $entry->ID_RUANGAN, 'Delete'));
                    $i++;
                }
                $display['content']=$this->table->generate();
            }else{
                $display['content']="Belum ada ruangan yang ditambahkan";
            }
            $data['title']="Daftar Ruangan Pelatihan";
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/ruangan_disp_view', $display);
            $this->load->view('footer_view');
        }
        function hapus_ruangan($id=''){
            $query1=$this->db->get_where('jadwal_pelatihan',array('ID_RUANGAN'=>$id));
            $query2=$this->db->get_where('daftar_jadwal',array('ID_RUANGAN'=>$id));
            if($query1->num_rows()>0||$query2->num_rows()>0){
                echo "Masih ada kelas yang terjadwal pada ruangan ini, ruangan tidak dapat dihapus <br/>";
                echo '<a href="javascript:back()">Kembali</a>';
            }else{
                $this->db->delete('daftar_ruangan',array('ID_RUANGAN'=>$id));
            }
            redirect(base_url() . 'admin/ruangan_con');
        }
        function tambah_ruangan(){
            $data['title']="Tambah Ruangan Pelatihan";
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/ruangan_form');
            $this->load->view('footer_view');
        }
        function insert(){
            $nama = $this->input->post('NAMA_RUANGAN');
            if($nama==''){
                echo 'harap isi nama ruangan <br/>';
                echo '<a href="javascript:back()">Kembali</a>';
            }else{
                $this->db->insert('daftar_ruangan',$_POST);
                redirect(base_url() . 'admin/ruangan_con');
            }
        }
        function edit_ruangan($id=''){
            $query=$this->db->get_where('daftar_ruangan',array('ID_RUANGAN'=>$id));
            $row=$query->row();
            $display['id'] = $row->ID_RUANGAN;
            $display['nama'] = $row->NAMA_RUANGAN;
            $display['kapasitas'] = $row->KAPASITAS;
            $data['title']="Edit Ruangan Pelatihan";
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/ruangan_edit',$display);
            $this->load->view('footer_view');
        }
        function update(){
            $nama = $this->input->post('NAMA_RUANGAN');
            if($nama==''){
                echo 'harap isi nama ruangan <br/>';
                echo '<a href="javascript:back()">Kembali</a>';
            }else{
                $id=$this->input->post('ID_RUANGAN');
                $this->db->where('ID_RUANGAN', $id);
                $this->db->update('daftar_ruangan', $_POST); 
                redirect(base_url() . 'admin/ruangan_con');
            }
        }
    }
?>
